<?php
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/db/BaseDAO.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/util/plog/classes/plog.php';

class PublishPayToUserDAO extends BaseDAO {
	
	public function __construct() {
		parent::__construct ();
		$this->log = Plog::factory ( __FILE__ );
	}
	public function insertData($attr){
		$sql=$this->genInserSQL('paytouser',$attr);
		$result = mysql_query ( $sql );
		return $result;
	}
	public function update($pid,$payId,$payDate,$payMoney,$status){
		$sql = "update paytouser set  
		  			 PayId='$payId',
		  			 PayDate=DATE_FORMAT('$payDate','%Y-%m-%d'),
		  			 PayMoney=$payMoney,
		  			 Status=$status 
		  			 where Pid = $pid";
		
		$result = mysql_query ( $sql );
		return $result;
	}

	public function queryAllDateByAccount($account,$start,$limit) {
		$sql = "select * from paytouser where account = '$account' limit $start,$limit";
//		echo $sql;
		$result = @mysql_query ( $sql );
		$items = $this::toArray ( $result );
		return $items;
	}
	
	public function getSumPayed($account){
		$sql="SELECT SUM(PayMoney) Sum FROM paytouser WHERE ACCOUNT='$account'";
//		echo $sql;
		$result = mysql_query ( $sql );
		$row=mysql_fetch_assoc($result);
		$sum=$row["Sum"];
		@mysql_free_result($result);
		if(!isset($sum)){
			$sum=0;
		}
		return $sum;
	}
	
	public function insertOrUpdate($attr){
		$pid=$attr["Pid"];
		$attr["Status"]=($attr["Status"])?1:0;
		if(""!=$pid && $this->checkDateIsExists($pid)){
			$this->update($pid,$attr["PayId"],$attr["PayDate"],$attr["PayMoney"],$attr["Status"]);
		}else{
			$this->insertData($attr);
		}
	}
	/**
	 * true 存在
	 * false 不存在
	 * Enter description here ...
	 * @param unknown_type $pid
	 */
	public function checkDateIsExists($pid){
		$sql="SELECT count(*) Count FROM paytouser WHERE Pid=$pid";
		$result = mysql_query ( $sql );
		$row=mysql_fetch_assoc($result);
		$count=$row["Count"];
		@mysql_free_result($result);
		if($count>0){
			return true;
		}
		return false;
		
	}
}

?>